package com.hjzd.ai.smartpractice.controller;

import com.hjzd.ai.smartpractice.entity.DTO.chat.ChatCompletionRequestDTO;
import com.hjzd.ai.smartpractice.service.ApiService;
import com.hjzd.ai.smartpractice.service.RedisService;
import com.hjzd.ai.smartpractice.util.res.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.Map;

/**
 * 智能对练控制器
 *
 * @author pengyonglei
 * @version 1.0.0
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/smart-practice")
public class SmartPracticeController {

	@Resource
	private ApiService apiService;
	@Resource
	private RedisService redisService;

	// 智能对练
	@PostMapping(value = "/v1/chat/completions", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	public Flux<String> chatCompletions(@RequestBody ChatCompletionRequestDTO requestDTO) {
		return apiService.chatCompletionsStream(requestDTO.getUniqueKey(), requestDTO.getChatLLMType(), requestDTO.getChatType(), requestDTO.getPrompt());
	}

	@PostMapping(value = "/v2/chat/completions", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	public void chatCompletions2(@RequestBody ChatCompletionRequestDTO requestDTO, HttpServletResponse response) {
		apiService.chatCompletionsStream2(requestDTO.getUniqueKey(), requestDTO.getPrompt(), requestDTO.getChatType(), response);
	}

//	@PostMapping(value = "/v1/chat/completions")
//	public String chatCompletions(@RequestBody ChatCompletionRequestDTO requestDTO) {
//		return ResponseUtil.getResponseJson(apiService.chatCompletions(requestDTO.getUniqueKey(), requestDTO.getPrompt()));
//	}

	@PostMapping("/scoreForChat")
	public String scoreForChat(@RequestBody Map<String, String> params) {
		String uniqueKey = params.get("uniqueKey");
		String audioText = params.get("audioText");
		String duration = params.get("duration");
		String id = params.get("id");
		String negativeValue = params.get("negativeValue");
		return ResponseUtil.getResponseJson(apiService.scoreForChat(uniqueKey, audioText, duration, id, negativeValue));
	}

	// 初始化对话信息
	@GetMapping("/init")
	public String init() {
		return ResponseUtil.getResponseJson(apiService.init());
	}

	// 录音转文字
	@PostMapping("/audioToText")
	public String audioToText(@RequestParam("file") MultipartFile file, @RequestParam("uniqueKey") String uniqueKey) {
		return ResponseUtil.getResponseJson(apiService.audioToText(uniqueKey, file));
	}


	@PostMapping("/scoreForAllChat")
	public String scoreForAllChat(@RequestBody Map<String,String> requestDTO) {
		String uniqueKey = requestDTO.get("uniqueKey");
		return ResponseUtil.getResponseJson(apiService.scoreForAllChat(uniqueKey));
	}

	@GetMapping("/exportTestResult")
	public String exportTestResult(@RequestParam("date") String date) {
		return ResponseUtil.getResponseJson(apiService.exportTestResult(date));
	}

	@GetMapping("/getCharacters")
	public String getCharacters() {
		return ResponseUtil.getResponseJson(apiService.getCharacters());
	}

	@PostMapping("/setCharacters")
	public String setCharacters(@RequestBody Map<String, String> params) {
		String uniqueKey = params.get("uniqueKey");
		String character = params.get("character");
		redisService.set("character_" + uniqueKey, character, 60 * 60 * 24 * 30);
		return ResponseUtil.getSuccessJson(true);
	}
}
